﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace ARCam3D.Imaging
{
    unsafe public class VectorImage : AbstractImage
    {
        private const int componentsCount = 2;
        private const int xOffset = 0;
        private const int yOffset = 1;

        public static int ComponenetsCount
        {
            get { return componentsCount; }
        }

        public static int XOffset
        {
            get { return xOffset; }
        }

        public static int YOffset
        {
            get { return yOffset; }
        }

        private IntPtr dataPtr;

        public VectorImage(int width, int height)
            : base(width, height)
        {
            this.dataPtr = Marshal.AllocHGlobal(width * height * componentsCount * sizeof(int));
        }

        ~VectorImage()
        {
            if (dataPtr != null && dataPtr != IntPtr.Zero)
            {
                Marshal.FreeHGlobal(dataPtr);
            }
        }

        public IntPtr DataPtr
        {
            get { return dataPtr; }
        }

        public int GetVectorX(int x, int y)
        {
            return ((int*)dataPtr.ToPointer())[((y * width + x) * componentsCount + xOffset)];
        }

        public int GetVectorY(int x, int y)
        {
            return ((int*)dataPtr.ToPointer())[((y * width + x) * componentsCount + yOffset)];
        }

        public void SetVector(int x, int y, int vectorX, int vectorY)
        {
            int* dataPointer = (int*)dataPtr.ToPointer();
            long offset = ((y * width + x) * componentsCount);
            dataPointer[offset + xOffset] = vectorX;
            dataPointer[offset + yOffset] = vectorY;
        }
    }
}
